package servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.award.jdbc.dao.JdbcDao;
import com.award.model.User;

public class login extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public login() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {		
		request.setCharacterEncoding("UTF-8");
		//获得登录方式
		String flag=request.getParameter("flag");		
		//获得session
		HttpSession session=request.getSession();
		//建立数据库连接
		JdbcDao jdbcDao=new JdbcDao();
//-------------------------------------------------------------------------------------------------------------------------------		
		//手机动态登录
		if(flag.equals("login_dynamic")){	
			String phone=request.getParameter("phone");
			try {
				//判断手机号是否已注册
				if (jdbcDao.isPhone(phone)) {
					System.out.println("已注册");
					String id=jdbcDao.getIdByPhone(phone);
					response.sendRedirect(request.getContextPath()+"/index.jsp");
					User user=null;
					try {
						user = jdbcDao.queryEmployeeByID(Integer.parseInt(id));
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					session.setAttribute("user", user);
				}else {
					System.out.println("未注册");
					User user=new User();
					//自动生成名字
					UUID name=UUID.randomUUID();
					String name1=name.toString().replaceAll("-", "");
					String name2="a"+name1.subSequence(25, name1.length()-1).toString();
					user.setUsername(name2);				
					user.setTelephone(request.getParameter("phone"));
					//默认游客图
					user.setUserImgURL("./images/traveller.jpg");
					session.setAttribute("user", user);
					jdbcDao.insertUser(user);	
					String path=request.getContextPath()+"/index.jsp";
					response.sendRedirect(path);
					session.setAttribute("user", user);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		
		
//------------------------------------------------------------------------------------------------------------------------------		
		//账号密码登录
		if(flag.equals("login_password")){
			String loginNum=request.getParameter("username");
			String identityType=null;
			//判断登录类型是手机号还是用户名
			for (int i = 0; i < loginNum.length(); i++){
				   if (!Character.isDigit(loginNum.charAt(i))){
				     identityType="username";break;
				   }else {
					identityType="phone";
				}
			}
			
			//用户名登录
			if (identityType.equals("username")) {
				try {
					String username=request.getParameter("username");
					boolean b=jdbcDao.isUsername(username);
					if (b) {
						if(request.getParameter("password").equals(jdbcDao.getPasswordByUsername(username))){
						String id=jdbcDao.getIdByUsername(username);
						User user=null;
						try {
							user=jdbcDao.queryEmployeeByID(Integer.parseInt(id));
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						session.setAttribute("user", user);
						response.sendRedirect(request.getContextPath()+"/index.jsp");
						}else {
							response.sendRedirect(request.getContextPath()+"/jsp/connectionWechat.jsp?err=wrongPw0");
						}
					}else{
					response.sendRedirect(request.getContextPath()+"/jsp/connectionWechat.jsp?err=wrongUsername");
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			//手机号登录
			else if(identityType.equals("phone")){
				String phone=request.getParameter("username");
				try {
					if(jdbcDao.isPhone(phone)){
						if (jdbcDao.getPasswordByPhone(phone).equals(request.getParameter("password"))) {
							String id=jdbcDao.getIdByPhone(phone);
							User user=null;
							try {
								user=jdbcDao.queryEmployeeByID(Integer.parseInt(id));
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							session.setAttribute("user", user);
							response.sendRedirect(request.getContextPath()+"/index.jsp");
						}else {
							response.sendRedirect(request.getContextPath()+"/jsp/connectionWechat.jsp?err=wrongPw1");
						}
					}else {
						response.sendRedirect(request.getContextPath()+"/jsp/connectionWechat.jsp?err=wrongUsername");
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
				  
				 
		}
		
//-----------------------------------------------------------------------------------------------------------------------------------
		//微信登录
		//还没写
		
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
